/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ht.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ht.entity.HtContract;
import com.thinkgem.jeesite.modules.ht.service.HtContractService;

/**
 * 租赁合同管理Controller
 * @author yy
 * @version 2016-12-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ht/htContract")
public class HtContractController extends BaseController {

	@Autowired
	private HtContractService htContractService;
	
	@ModelAttribute
	public HtContract get(@RequestParam(required=false) String id) {
		HtContract entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = htContractService.get(id);
		}
		if (entity == null){
			entity = new HtContract();
		}
		return entity;
	}
	
	@RequiresPermissions("ht:htContract:view")
	@RequestMapping(value = {"list", ""})
	public String list(HtContract htContract, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HtContract> page = htContractService.findPage(new Page<HtContract>(request, response), htContract); 
		model.addAttribute("page", page);
		return "modules/ht/htContractList";
	}

	@RequiresPermissions("ht:htContract:view")
	@RequestMapping(value = "form")
	public String form(HtContract htContract, Model model) {
		model.addAttribute("htContract", htContract);
		return "modules/ht/htContractForm";
	}

	@RequiresPermissions("ht:htContract:edit")
	@RequestMapping(value = "save")
	public String save(HtContract htContract, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, htContract)){
			return form(htContract, model);
		}
		htContractService.save(htContract);
		addMessage(redirectAttributes, "保存租赁合同管理成功");
		return "redirect:"+Global.getAdminPath()+"/ht/htContract/?repage";
	}
	
	@RequiresPermissions("ht:htContract:edit")
	@RequestMapping(value = "delete")
	public String delete(HtContract htContract, RedirectAttributes redirectAttributes) {
		htContractService.delete(htContract);
		addMessage(redirectAttributes, "删除租赁合同管理成功");
		return "redirect:"+Global.getAdminPath()+"/ht/htContract/?repage";
	}

}